package shc.graduation.http.manager;

import android.os.AsyncTask;

import java.util.concurrent.Executor;

import shc.graduation.bean.StateEvent;
import shc.graduation.http.listener.QueryListener;
import shc.graduation.http.query.BaseQuery;
import shc.graduation.http.query.CodeQuery;
import shc.graduation.http.query.MyCollectionQuery;
import shc.graduation.http.query.PaoWangArticleQuery;
import shc.graduation.http.query.PaperInfoQuery;

/**
 * Created by shuhuiChen on  2016/5/21  18:29.
 * Email ：shuhuiCSH@126.com
 */
public class QueryManager {

    private Executor mExecutor = AsyncTask.THREAD_POOL_EXECUTOR;
    private static QueryManager mQueryManager = null;

    public static final int OPEN_CODE = 0;
    public static final int PAOWANG_ARTICLE = 1;
    public static final int PEPER_ARTICLE = 2;
    public static final int MYCOLLECTION = 3;

    private BaseQuery mBaseQuery = null;

    private QueryManager() {

    }

    public static QueryManager getInstance() {
        if (mQueryManager == null) {
            synchronized (QueryManager.class) {
                if (mQueryManager == null) {
                    mQueryManager = new QueryManager();
                }
            }
        }
        return mQueryManager;
    }

    public void addQuery(StateEvent stateEvent, QueryListener queryListener) {
        if (stateEvent == null || mExecutor == null) {
            return;
        }

        switch (stateEvent.project) {

            //查找开源项目
            case OPEN_CODE:
                mBaseQuery = new CodeQuery();

                break;

            //查找泡网文章
            case PAOWANG_ARTICLE:
                mBaseQuery = new PaoWangArticleQuery();

                break;

            //查找好东西日报
            case PEPER_ARTICLE:
                mBaseQuery = new PaperInfoQuery();

                break;

            //查找我的收藏
            case MYCOLLECTION:
                mBaseQuery = new MyCollectionQuery();

                break;

            default:

                break;
        }

        mBaseQuery.setStateEvent(stateEvent);
        mBaseQuery.setQueryListener(queryListener);
        mExecutor.execute(mBaseQuery);
    }

}
